#ifndef _WIFI_HPP
#define _WIFI_HPP

#include "usart.h"
#include <string.h>
#include <stdio.h>

#define ESC     0x1B

#define WWPA    "AT+WWPA"
#define WA      "AT+WA"
#define NCTCP   "AT+NCTCP"
#define NDHCP   "AT+NDHCP"
#define WD      "AT+WD"


void cmd ( char* cmd, char* parameter ) {
  char strcmd[256];
  if ( strlen (parameter) != 0 )
    sprintf ( strcmd, "%s%s%s%s" , cmd, "=", parameter, "\n" ); 
  else
    sprintf ( strcmd, "%s%s" , cmd, "\n" ); 
  
  writeString ( USART3, strcmd );
}
void dhcpset () {
  writeString ( USART3, "AT+NDHCP=1\n" );
  delay ( 10000000 );
}
void wifiConnect ( char* ssid, char* password ) {
  char strcmd[256];
  delay ( 30000000 );
  //pass word setting
  sprintf ( strcmd, "%s%s%s" , "AT+WWPA=", password, "\n" );  
  writeString ( USART3, strcmd );
  
  delay ( 30000000 );
  //ap
  sprintf ( strcmd, "%s%s%s" , "AT+WA=", ssid, "\n" );
  writeString ( USART3, strcmd );
  
  delay ( 30000000 );
  

}
void tcpConnect ( char* ip, char* port ) {
  char strcmd[256];
  sprintf ( strcmd, "%s%s%s%s%s" , "AT+nctcp=", ip, "'", port, "\n" );  
  writeString ( USART3, "AT+nctcp=" );
  writeString ( USART3, ip);
  writeByte ( USART3, ',' );
  writeString ( USART3, port);
  writeByte ( USART3, '\n' );
  
  
  delay ( 30000000 );
}
void wifiSendData ( char* data ){ 
  char cmdstr[512];
  
  sprintf ( cmdstr, "%c%s%s%c%s", ESC, "S0", data, ESC, "E\n" ); 
  
  writeString ( USART3, cmdstr );
  delay ( 3000000 );
  
}
void disconnect () {
  writeString(USART3, "AT+WD\n");
  delay ( 3000000 );
}




#endif